package org.jeecg.modules.service.impl;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.entity.DbContract;
import org.jeecg.modules.entity.DbYearlyPlan;
import org.jeecg.modules.mapper.DbYearlyPlanMapper;
import org.jeecg.modules.service.IDbContractService;
import org.jeecg.modules.service.IDbYearlyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

/**
 * @Description: db_yearly_plan
 * @Author: jeecg-boot
 * @Date:   2024-08-27
 * @Version: V1.0
 */
@Service
public class DbYearlyPlanServiceImpl extends ServiceImpl<DbYearlyPlanMapper, DbYearlyPlan> implements IDbYearlyPlanService {

    @Autowired
    private IDbContractService dbContractService;
    /**
     * 修改年度计划已签单额
     */
    @Transactional
    @Override
    public void addContractUpdateYearPlan(DbContract dbContract){
        LocalDate signatureDate = dbContract.getSignatureDate();

        LocalDate firstDayOfYear  = signatureDate.withDayOfYear(1);
        LocalDate lastDayOfYear = signatureDate.withMonth(12).withDayOfMonth(31);
        List<DbContract> dbContracts = dbContractService.list(Wrappers.<DbContract>lambdaQuery()
                .le(DbContract::getSignatureDate,lastDayOfYear)
                .ge(DbContract::getSignatureDate,firstDayOfYear));
        BigDecimal tolAmount = null;
//        年度合同金额加和
        if(CollectionUtils.isNotEmpty(dbContracts)){
            tolAmount = dbContracts.stream().map(DbContract::getContractAmount)
                    .reduce(BigDecimal.ZERO,BigDecimal::add);
        }
        DbYearlyPlan dbYearlyPlan = this.getOne(Wrappers.<DbYearlyPlan>lambdaQuery().eq(DbYearlyPlan::getPlanYear, signatureDate.getYear()));
        BigDecimal planOrder = dbYearlyPlan.getPlanOrder();
//        计算签单进度
        BigDecimal divide = tolAmount.divide(planOrder, 4, RoundingMode.HALF_UP);
        double orderProgress = divide.doubleValue() * 100;
        this.update(Wrappers.<DbYearlyPlan>lambdaUpdate()
                .set(DbYearlyPlan::getOrdered,tolAmount)
                .set(DbYearlyPlan::getOrderProgress,orderProgress)
                .eq(DbYearlyPlan::getPlanYear,signatureDate.getYear()));
    }

    @Override
    @Transactional
    public void updateRepayProgress(String contractId) {
        DbContract dbContract = dbContractService.getOne(Wrappers.<DbContract>lambdaQuery().eq(DbContract::getContractId,contractId));
        LocalDate signatureDate = dbContract.getSignatureDate();
        LocalDate firstDayOfYear = signatureDate.withDayOfYear(1);
        LocalDate lastDayOfYear = signatureDate.withMonth(12).withDayOfMonth(31);
        List<DbContract> dbContracts = dbContractService.list(Wrappers.<DbContract>lambdaQuery()
                .le(DbContract::getSignatureDate,lastDayOfYear)
                .ge(DbContract::getSignatureDate,firstDayOfYear));
        BigDecimal tolRebackAmount = null;
//        年度合同返回金额加和
        if(CollectionUtils.isNotEmpty(dbContracts)){
            tolRebackAmount = dbContracts.stream().map(DbContract::getReturnedAmount)
                    .reduce(BigDecimal.ZERO,BigDecimal::add);
        }
        DbYearlyPlan dbYearlyPlan = this.getOne(Wrappers.<DbYearlyPlan>lambdaQuery().eq(DbYearlyPlan::getPlanYear, signatureDate.getYear()));
        BigDecimal planReback = dbYearlyPlan.getPlanReback();
//        计算签单进度
        BigDecimal divide = tolRebackAmount.divide(planReback, 4, RoundingMode.HALF_UP);
        double rebackProgress = divide.doubleValue() * 100;
        this.update(Wrappers.<DbYearlyPlan>lambdaUpdate()
                .set(DbYearlyPlan::getRebacked,tolRebackAmount)
                .set(DbYearlyPlan::getRebackProgress,rebackProgress)
                .eq(DbYearlyPlan::getPlanYear,signatureDate.getYear()));
    }
}
